package com.umfb.bootstrap.intercepter;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by gzheng on 17-11-17.
 */
public class LoginInterceptor extends AbstractInterceptor implements HandlerInterceptor {
    //Token 超期时间，单位:秒；
    private static Integer tokenExpireTimeSeconds = 7*24*60*60;// 30分钟

    Logger logger = Logger.getLogger(LoginInterceptor.class);

    public static Integer getTokenExpireTimeSeconds() {
        return tokenExpireTimeSeconds;
    }

    public static void setTokenExpireTimeSeconds(Integer tokenExpireTimeSeconds) {
        LoginInterceptor.tokenExpireTimeSeconds = tokenExpireTimeSeconds;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Boolean validate = true;
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:ss:mm").format(new Date()));
        logger.info("登录拦截器的拦截时间;time:{}"+new SimpleDateFormat("yyyy-MM-dd HH:ss:mm").format(new Date()));
//        response.sendRedirect("/login");
        return validate;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        super.afterConcurrentHandlingStarted(request, response, handler);
    }
}
